<template>
  <div class="device-card curtain-device" :class="{ 'device-on': device.status === 'on' }">
    <div class="device-header">
      <div class="device-icon">
        <el-icon :size="24">
          <Grid />
        </el-icon>
      </div>
      <div class="device-info">
        <h3>{{ device.name }}</h3>
        <p>{{ device.room }}</p>
      </div>
      <div class="device-controls">
        <el-switch
          v-model="isOn"
          @change="toggleDevice"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </div>
    </div>
    
    <div v-if="device.status === 'on'" class="device-details">
      <div class="control-group">
        <label>位置</label>
        <div class="position-control">
          <el-button @click="setPosition(0)" :disabled="device.position === 0">
            <el-icon><Close /></el-icon>
            关闭
          </el-button>
          <span class="position-display">{{ device.position }}%</span>
          <el-button @click="setPosition(100)" :disabled="device.position === 100">
            <el-icon><Open /></el-icon>
            打开
          </el-button>
        </div>
        <el-slider
          :model-value="device.position"
          :min="0"
          :max="100"
          :step="10"
          @input="updatePosition"
          show-input
          :show-input-controls="false"
        />
      </div>
      
      <div class="control-group">
        <label>快捷操作</label>
        <div class="quick-actions">
          <el-button @click="setPosition(25)" size="small">1/4</el-button>
          <el-button @click="setPosition(50)" size="small">1/2</el-button>
          <el-button @click="setPosition(75)" size="small">3/4</el-button>
        </div>
      </div>
    </div>
    
    <div class="device-actions">
      <el-button type="danger" size="small" @click="removeDevice">
        <el-icon><Delete /></el-icon>
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'
// eslint-disable-next-line no-unused-vars
import { Grid, Delete, Close, Open } from '@element-plus/icons-vue'
import { useDeviceStore } from '../../store/deviceStore.js'

export default {
  name: 'CurtainDevice',
  props: {
    device: {
      type: Object,
      required: true
    }
  },
  emits: ['update:device'],
  setup(props, { emit }) {
    const deviceStore = useDeviceStore()
    
    const isOn = computed({
      get: () => props.device.status === 'on',
      set: (value) => {
        if (value) {
          deviceStore.updateDeviceStatus(props.device.id, 'on')
        } else {
          deviceStore.updateDeviceStatus(props.device.id, 'off')
        }
      }
    })
    
    const toggleDevice = () => {
      deviceStore.toggleDevice(props.device.id)
    }
    
    const updatePosition = async (value) => {
      console.log('更新位置:', { deviceId: props.device.id, position: value })
      try {
        // 先更新本地状态，提供即时反馈
        const updatedDevice = { ...props.device, position: value }
        emit('update:device', updatedDevice)
        
        // 然后尝试更新到后端
        await deviceStore.updateDeviceProperty(props.device.id, 'position', value)
        console.log('位置更新成功')
      } catch (error) {
        console.error('更新位置失败:', error)
      }
    }
    
    const setPosition = (position) => {
      updatePosition(position)
    }
    
    const removeDevice = () => {
      deviceStore.removeDevice(props.device.id)
    }
    
    return {
      isOn,
      toggleDevice,
      updatePosition,
      setPosition,
      removeDevice
    }
  }
}
</script>

<style scoped>
.device-card {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  margin: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.device-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.device-card.device-on {
  border-color: #e6a23c;
  background: linear-gradient(135deg, #fef9e7 0%, #fdf6ec 100%);
}

.device-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.device-icon {
  margin-right: 15px;
  color: #666;
}

.device-on .device-icon {
  color: #e6a23c;
}

.device-info h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.device-info p {
  margin: 5px 0 0 0;
  color: #666;
  font-size: 14px;
}

.device-controls {
  margin-left: auto;
}

.device-details {
  margin: 15px 0;
  padding: 15px;
  background: rgba(230, 162, 60, 0.05);
  border-radius: 8px;
}

.control-group {
  margin-bottom: 15px;
}

.control-group:last-child {
  margin-bottom: 0;
}

.control-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
}

.position-control {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
  margin-bottom: 10px;
}

.position-display {
  font-size: 20px;
  font-weight: bold;
  color: #e6a23c;
  min-width: 60px;
  text-align: center;
}

.quick-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.device-actions {
  margin-top: 15px;
  text-align: right;
}
</style>
